package com.hiflying.commons.utils;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;

import androidx.core.view.ViewCompat;

import com.accloud.utils.LocalUtils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.List;

public class SmartLinkUtils {
    public static String getBroadcastAddress(Context ctx) {
        List<InterfaceAddress> interfaceAddresses;
        WifiManager wifiManager = (WifiManager) ctx.getSystemService("wifi");
        int ipInt = 0;
        int netmaskInt = 0;
        DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
        if (dhcpInfo != null) {
            ipInt = dhcpInfo.ipAddress;
            netmaskInt = dhcpInfo.netmask;
            if (!(ipInt == 0 || netmaskInt == 0)) {
                try {
                    return calculateBroadcastIpAddress(ipInt, netmaskInt);
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
            }
        } else {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            if (wifiInfo != null) {
                ipInt = wifiInfo.getIpAddress();
            }
        }
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            if (networkInterfaces != null) {
                while (networkInterfaces.hasMoreElements()) {
                    NetworkInterface networkInterface = networkInterfaces.nextElement();
                    if (networkInterface.getName().trim().toLowerCase().startsWith("wlan") && (interfaceAddresses = networkInterface.getInterfaceAddresses()) != null && !interfaceAddresses.isEmpty()) {
                        for (InterfaceAddress interfaceAddress : interfaceAddresses) {
                            InetAddress broadcastInetAddress = interfaceAddress.getBroadcast();
                            if (broadcastInetAddress != null && (broadcastInetAddress instanceof Inet4Address)) {
                                String ip = broadcastInetAddress.getHostAddress();
                                if (!TextUtils.isEmpty(ip)) {
                                    return ip;
                                }
                            }
                        }
                        continue;
                    }
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        if (ipInt == 0) {
            return LocalUtils.HOST_BROADCAST;
        }
        if (netmaskInt == 0) {
            netmaskInt = ViewCompat.MEASURED_SIZE_MASK;
        }
        try {
            return calculateBroadcastIpAddress(ipInt, netmaskInt);
        } catch (UnknownHostException e3) {
            e3.printStackTrace();
            return LocalUtils.HOST_BROADCAST;
        }
    }

    public static String calculateBroadcastIpAddress(int ip, int netmask) throws UnknownHostException {
        int broadcastInt = (ip & netmask) | (netmask ^ -1);
        byte[] broadcastIpAddress = new byte[4];
        for (int i = 0; i < 4; i++) {
            broadcastIpAddress[i] = (byte) ((broadcastInt >> (i * 8)) & 255);
        }
        return InetAddress.getByAddress(broadcastIpAddress).getHostAddress();
    }
}
